package net.example.model;

import com.oraycn.esframework.core.ClientType;
import com.oraycn.omcs.utils.SerializeUtils;

import java.io.IOException;
import java.nio.ByteOrder;
import java.util.Date;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * Created by ZN on 2016/1/22.
 */
public class OfflineMessage {
    private int OfflineMessageLen;
    private String destUserID;
    private String groupID;
    private byte[] information;
    private int informationType;
    private String sourceUserID;
    private Date Time;
    private String Tag;
    private ClientType sourceType = ClientType.DOT_NET;
    public ClientType getSourceType(){return sourceType;}

    public String getTag(){return Tag;}

    public String getDestUserID() {
        return destUserID;
    }
    public String getGroupID() {
        return groupID;
    }
    public byte[] getInformation() {
        return information;
    }
    public int getInformationType() {
        return informationType;
    }
    public String getSourceUserID() {       return sourceUserID;    }
    public Date getTime() {
        return Time;
    }

    public void deserialize(byte[] bytes) throws IOException {
        ByteBuf buffer = Unpooled.wrappedBuffer(bytes).order(ByteOrder.LITTLE_ENDIAN);
        deSerialize(buffer);
    }

    public void deSerialize(ByteBuf buffer)throws IOException
    {
        this.OfflineMessageLen = buffer.readInt();
        this.destUserID = SerializeUtils.readStrIntLen(buffer);
        this.groupID = SerializeUtils.readStrIntLen(buffer);
        int informationLen = buffer.readInt();
        information = new byte[informationLen];
        buffer.readBytes(information);
        this.informationType = buffer.readInt();
        this.sourceType=ClientType.values()[buffer.readInt()];
        this.sourceUserID = SerializeUtils.readStrIntLen(buffer);
        this.Tag= SerializeUtils.readStrIntLen(buffer);
        this.Time = new Date(buffer.readLong()- 8*3600*1000);//java 为1970年1月1日 8点0分0秒 ；C#为1970年1月1日 0点0分0秒
    }
}
